<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1100px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="120px" :disabled="disableEditSubmit || disableSave">
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>1、【合同】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同所属组织" prop="contractorgname" :required="true" :rules="rules.validateRequire">
            <tl-ref-org v-model="model.contractorgname" :initDisplayData.sync="model.contractorgname" dataType="code" :singleSelect="true" @confirm="onSelectOrg" :disabled="disableEditSubmit || disableSave"></tl-ref-org>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <!-- <el-col :span="12">
                    <el-form-item label="合同所属组织编码" prop="contractorgcode">
                        <el-input v-model="model.contractorgcode"></el-input>
                    </el-form-item>
                </el-col> -->
        <el-col :span="12">
          <el-form-item label="合同所属部门" prop="contractdeptname" :required="true" :rules="rules.validateRequire">
            <tl-ref-dept v-model="model.contractdeptname" :initDisplayData.sync="model.contractdeptname" dataType="code" nameType="fullname" :singleSelect="true" @confirm="onSelectDept" :disabled="disableEditSubmit || disableSave" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customername" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.customername" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCustomerRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
                 <el-col :span="12">
                    <el-form-item label="合同所属部门编码" prop="contractdeptcode">
                        <el-input v-model="model.contractdeptcode"></el-input>
                    </el-form-item>
                </el-col>
                 <el-col :span="12">
                    <el-form-item label="客户" prop="customerid">
                        <el-input v-model="model.customerid"></el-input>
                    </el-form-item>
                </el-col>
            </el-row> -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同名称" prop="contractname" :rules="rules.validateRequire">
            <el-input v-model="model.contractname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同日期" prop="contractdate" :rules="rules.validateRequire">
            <el-date-picker v-model="model.contractdate" type="date" placeholder="合同日期" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
                    <el-form-item label="合同编号" prop="contractcode">
                        <el-input v-model="model.contractcode" :readonly="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="本次合同金额" prop="contractmoney" :rules="rules.validateRequire">
            <el-input-number v-model="model.contractmoney" controls-position="right" :step="1" :precision="2" style="width:95%"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="11" :offset="1">
          <el-alert title="提示：若本次合同金额为负值，则项目合同总金额会减去相应值。" type="warning" :closable="false">
          </el-alert>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收件人" prop="receiver" :rules="rules.validateRequire">
            <el-input v-model="model.receiver"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人电话" prop="phone" :rules="rules.validateRequire">
            <el-input v-model="model.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="回寄地址" prop="postal" :rules="rules.validateRequire">
            <el-input v-model="model.postal"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="快递单号" prop="couriernumber" v-show="model.state == '9' || model.couriernumber != null">
            <el-input v-model="model.couriernumber"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="快递方式" prop="courier" v-show="model.state == '9' || model.courier != null">
            <el-select v-model="model.courier" filterable clearable>
              <el-option v-for="item in codeModel.courierList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="deptname">
                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="orgname">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人" prop="username">
                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="usercode">
                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate" :rules="rules.validateRequire">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmContract" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <tl-attachment ref="attachmentRef" sourceType="PmContract" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
      <tl-attachment ref="attachmentSignRef" sourceType="PmSignContract" :sourceId="model.id" btnTitle="合同扫描件" :disabled="disableAttachment" :editable="true"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    <tlpm-customer-ref ref="pmCustomerRef" @confirm="getpmCustomerRefData" :isSingleSelect="true"></tlpm-customer-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmCustomerRef from '@/views/ProjectManage/pmCustomer/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmProjectRef, tlpmCustomerRef },
  data() {
    return {
      moduleTitle: '项目合同',
      urlPrefix: '/pm/contract/',
      url: {
        // 额外的url
      }
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        // if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        //     this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        // }
        // this.$set(this.model, 'orgcode', this.org.code)
        // this.$set(this.model, 'orgname', this.org.name)
        // this.$set(this.model, 'deptcode', this.dept.code)
        // this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'docdate', new Date())
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    onSelectDept(selectedDepts) {
      const selectedDept = selectedDepts[0]
      this.$set(this.model, 'contractdeptname', selectedDept.fullname)
      this.$set(this.model, 'contractdeptcode', selectedDept.code)
    },
    onSelectOrg(selectedOrgs) {
      const selectedOrg = selectedOrgs[0]
      this.$set(this.model, 'contractorgname', selectedOrg.name)
      this.$set(this.model, 'contractorgcode', selectedOrg.code)
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'projectid', selectionRow.id)
      this.$set(this.model, 'dispprojectid', selectionRow.name)
      this.$set(this.model, 'contractdeptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'contractdeptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'contractorgname', selectionRow.ownerorgname)
      this.$set(this.model, 'contractorgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'deptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'deptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'orgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'orgname', selectionRow.ownerorgname)
    },
    /** 打开客户名称参照 */
    openpmCustomerRefDialog() {
      this.$refs.pmCustomerRef.visible = true
    },
    /** 获取客户名称参数数据 */
    getpmCustomerRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'customerid', selectionRow.id)
      this.$set(this.model, 'customername', selectionRow.name)
    },
    /** 提交审批前*/
    beforeApproval() {
      if (this.$refs.attachmentRef.fileList.length === 0) {
        this.$tap.alert('请上传合同附件！', 'warning')
        this.submitLoading = false
        return false
      }
      return true
    },
    saveAttachment(resolve, reject) {
      if (this.$refs.attachmentSignRef.fileList.length === 0) {
        this.$tap.alert('请上传合同扫描件！', 'warning')
        reject()
        return
      }
      resolve()
      // const { id, couriernumber, courier } = this.model
      // if (!couriernumber) {
      //     this.$tap.alert('请填写快递单号')
      //     reject()
      // }
      // if (!courier) {
      //     this.$tap.alert('请填写快递方式')
      //     reject()
      // }
      // const param = {
      //     id,
      //     couriernumber,
      //     courier
      // }
      // putAction(this.url.edit, param)
      //     .then(res => {
      //         if (res.success) {
      //             resolve()
      //         } else {
      //             this.$tap.alert(res.message, 'warning')
      //             reject()
      //         }
      //     })
      //     .catch(err => {
      //         this.$tap.alert('保存快递信息失败，请联系系统管理员', 'warning')
      //         reject()
      //     })
    }
    /** 提交审批 */
    // doSubmitApproval() {
    //     if (!this.url.submit) {
    //         this.$tap.alert('请设置审批地址!', 'warning')
    //         return
    //     }
    //     this.submitLoading = true
    //     this.$tap
    //         .confirm('确定提交审批吗？提交后，单据内容不可修改！')
    //         .then((_) => {
    //             if (!this.beforeApproval()) {
    //                 return
    //             }
    //             if (this.$refs.attachmentRef.fileCount === 0) {
    //                 this.$tap.alert('请上传合同附件！', 'warning')
    //                 return
    //             }
    //             putAction(this.url.submit, this.model).then((res) => {
    //                 if (res.success) {
    //                     this.$tap.message(res.message, 'success')
    //                     this.queryData(this.model.id)
    //                     this.afterApproval()
    //                 } else {
    //                     this.$tap.alert(res.message, 'warning')
    //                 }
    //                 this.submitLoading = false
    //             })
    //         })
    //         .catch((_) => {
    //             this.submitLoading = false
    //         })
    // }
  }
}
</script>

